<template>
  <div class="role">
    <el-button
      type="primary"
      size="small"
      @click="addModify({ pop_title: '新增' })"
    >
      新增
    </el-button>
    <el-table :data="roleData" class="mar-t20" border>
      <el-table-column prop="name" align="center" label="角色名称">
      </el-table-column>
      <el-table-column
        prop="describe"
        align="center"
        label="角色描述"
      ></el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button
            @click="addModify({ pop_title: '修改', role_info: scope.row })"
            size="small"
            type="text"
            class="warning-col"
          >
            修改
          </el-button>
          <el-button
            @click="deleteBtn(scope.row, index)"
            size="small"
            type="text"
            class="danger-col"
          >
            删除
          </el-button>
          <el-button
            @click="rolePower(scope.row)"
            size="small"
            type="text"
            class="success-col"
          >
            角色权限
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 新增，修改弹窗 -->
    <el-dialog
      :title="pop_title + '角色'"
      :visible.sync="addModifyPop"
      width="30%"
      :before-close="handleClose"
    >
      <el-form
        :model="roleForm"
        :rules="rules"
        ref="roleForm"
        label-position="left"
      >
        <el-form-item label="角色名称" label-width="80px" prop="name">
          <el-input size="small" v-model="roleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" label-width="80px" prop="describe">
          <el-input size="small" v-model="roleForm.describe"></el-input>
        </el-form-item>
      </el-form>
      <el-row type="flex" justify="end">
        <el-button type="primary" size="small" @click="submitRole('roleForm')">
          确定
        </el-button>
      </el-row>
    </el-dialog>
    <!-- 权限弹框 -->
    <el-dialog
      title="角色权限编辑"
      :visible.sync="powerPop"
      width="20%"
      :before-close="handleClose"
    >
      <RolePowerPop :roleId="roleVal" v-if="powerPop"></RolePowerPop>
    </el-dialog>
  </div>
</template>
<script>
import { roleJson } from "./roleJson"
import RolePowerPop from "./components/role-power-pop"
export default {
  components: {
    RolePowerPop,
  },
  data() {
    return {
      roleData: roleJson, //角色列表
      pop_title: "", //新增修改弹框标题
      addModifyPop: false, //新增修改弹框
      roleForm: {}, //新增修改表单内容
      //表单校验规则
      rules: {
        name: [{ required: true, message: "请输入角色名称", trigger: "blur" }],
      },
      powerPop: false, //角色权限弹框
      roleVal: "", //传入角色权限子组件的值
    }
  },
  mounted() {},
  methods: {
    // 新增修改
    addModify(item) {
      this.pop_title = item.pop_title
      this.addModifyPop = true
      this.roleForm = { ...item.role_info }
    },
    // 新增修改提交
    submitRole(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert("submit!")
        }
      })
    },
    // 删除
    deleteBtn(item, index) {
      console.log(item)
      this.$confirm("确认删除？")
        .then(() => {
          this.roleData.splice(index, 1)
        })
        .catch(() => {})
    },
    // 角色权限
    rolePower(item) {
      this.roleVal = item.id
      this.powerPop = true
    },
    // 关闭弹框
    handleClose(done) {
      done()
    },
  },
}
</script>
